package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.FilletImageView;
import com.wude.bean.GiftInfo;
import com.wude.guild.GiftDetActivity;
import com.wude.guild.R;
import com.wude.guild.face.GetGiftFace;
import com.wude.guild.views.RoundAngleImageView;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 唐小妹 on 2017/11/29 0029.
 */

public class GiftHolder extends BaseHolder<GiftInfo> implements View.OnClickListener {
    @BindView(R.id.gift_icon)
    RoundAngleImageView gift_icon;
    @BindView(R.id.tv_gameName)
    TextView tv_gameName;
    @BindView(R.id.tv_desribe)
    TextView tv_desribe;
    @BindView(R.id.tv_giftName)
    TextView tv_giftName;
    @BindView(R.id.tv_jieshao)
    TextView tv_jieshao;
    @BindView(R.id.tv_get)
    TextView tv_get;//领取

    private GiftInfo giftInfo;
    private GetGiftFace face;

    public GiftHolder(GetGiftFace face) {
        this.face = face;
    }

    @Override
    protected void refreshView(GiftInfo giftInfo, int position, Activity activity) {
        this.giftInfo = giftInfo;
        Utils.Fill(gift_icon, giftInfo.icon);
        tv_gameName.setText(giftInfo.game_name + "");
        tv_desribe.setText(giftInfo.introduction + "");
        tv_giftName.setText(giftInfo.giftbag_name + ":");
        tv_jieshao.setText(giftInfo.desribe + "");

        if ("0".equals(giftInfo.receive)) {
            //未领取
            tv_get.setText("领取");
            tv_get.setTextColor(activity.getResources().getColor(R.color.white));
            tv_get.setBackgroundResource(R.drawable.blue_corners_bg_5);
        } else {
            tv_get.setText("已领取");
            tv_get.setTextColor(activity.getResources().getColor(R.color.text_5050));
            tv_get.setBackgroundResource(R.color.transparency);
        }
        tv_get.setOnClickListener(this);
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.holder_gift, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    @Override
    public void onClick(View view) {
        //领取礼包
        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        map.put("gift_id", giftInfo.gift_id + "");
        HttpCom.POST(handler, HttpCom.getActivityGetNovice(), map, false);
    }

    /**
     * 领取礼包
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        ToastUtil.showToast(jsonObject.getString("return_code"));
                        if (1 == status) {
                            face.getSuccess();
                            giftInfo.receive = "1";
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    break;
            }
        }

    };
}
